package io.vertx.examples.spring.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

/**
 * 统一API响应结果封装
 */
public class Result<T> {

    //小诺用的是 wget 获取解析的文件，因此对json结果顺序有一定的要求
    @JSONField(ordinal = 1)
    private int code;
    @JSONField(ordinal = 2)
    private String message;
    @JSONField(ordinal = 3)
    private T data;
    @JSONField(ordinal = 4)
    private String data_len="1234567890";

    public Result setCode(ResultCode resultCode) {
        this.code = resultCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public Result setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    @JSONField(ordinal = 5)
    public boolean isSuccess() {
    	return code == 0;
    }
    public boolean notSuccess() {
    	return code != 0;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public String getData_len() {
        return data_len;
    }

    public void setData_len(String data_len) {
        this.data_len = data_len;
    }
}
